package response

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"message"`
}

const (
	ERROR   = 1
	SUCCESS = 0
)

func Result(code int, data interface{}, msg string, c *gin.Context) {
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
	})
}

func ResultWithHttpCode(httpCode int, data interface{}, msg string, c *gin.Context) {
	c.JSON(httpCode, Response{
		httpCode,
		data,
		msg,
	})
}

func Ok(c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, "成功", c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, message, c)
}

func OkWithData(data interface{}, c *gin.Context) {
	Result(SUCCESS, data, "成功", c)
}

func OkWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(SUCCESS, data, message, c)
}

func Fail(c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, "失败", c)
}

func FailWith401(c *gin.Context) {
	ResultWithHttpCode(http.StatusUnauthorized, map[string]interface{}{}, "请先登录", c)
}
func FailWith403(c *gin.Context) {
	ResultWithHttpCode(http.StatusForbidden, map[string]interface{}{}, "无权限", c)
}

func FailWith406(c *gin.Context) {
	ResultWithHttpCode(http.StatusNotAcceptable, map[string]interface{}{}, "参数错误", c)
}

func FailWith404(c *gin.Context) {
	ResultWithHttpCode(http.StatusNotFound, map[string]interface{}{}, "不存在", c)
}

func FailWithMessage(message string, c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, message, c)
}

func FailWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(ERROR, data, message, c)
}
